<?php
class PreparationREQAdministrateur
{
    //Variables qui contiendront la  pr�paration des requ�tes
    private $ReqSelectById;
    private $ReqSelectAll;
    private $ReqTrouverID;
    private $ReqUpdate;
    private $ReqInsert;
    private $ReqTruncate;
  	private $ReqSelectPhotoProfilByAdminId;
  	private $ReqSelectIdentifiantsById;


    public function __construct($db)
    {
    	//variables renseign�es lors de la pr�paration des requ�tes
	
        $this->ReqSelectAll = $db->prepare("SELECT * FROM administrateur ORDER BY ADMIN_NOM DESC");
		
        $this->ReqSelectById = $db->prepare("SELECT * FROM administrateur WHERE ADMIN_ID = ?");
        
 	    $this->ReqTrouverID = $db->prepare("SELECT ADMIN_ID FROM administrateur WHERE ADMIN_LOGIN = ? AND ADMIN_MDP = ?");
 	    
 	    $this->ReqUpdate = $db->prepare("UPDATE administrateur SET ADMIN_NOM = ?, ADMIN_PRENOM = ?, ADMIN_TEL = ?, ADMIN_EMAIL = ?  WHERE ADMIN_ID= ?");
 	    
		$this->ReqInsert = $db->prepare("INSERT INTO administrateur(ADMIN_NOM, ADMIN_PRENOM, ADMIN_FONCTION, ADMIN_TEL, ADMIN_EMAIL, ADMIN_LOGIN, ADMIN_MDP) VALUES (:ADMIN_NOM, :ADMIN_PRENOM, :ADMIN_FONCTION, :ADMIN_TEL, :ADMIN_EMAIL, :ADMIN_LOGIN, :ADMIN_MDP)");
		
       $this->ReqTruncate = $db->prepare("TRUNCATE article");
       
       $this->ReqSelectPhotoProfilByAdminId = $db->prepare("SELECT * FROM photoprofil WHERE PHO_ID_POSSEDER = ?");
       
       $this->ReqSelectIdentifiantsById = $db->prepare ("SELECT ADMIN_LOGIN AS 'Login', ADMIN_MDP AS 'MDP' FROM administrateur WHERE ADMIN_ID = ?");
	   
    }

//M�thodes publiques qui ex�cuteront les requ�tes d�j� pr�par�es dans le constructeur.
	public function insert($vnom, $vprenom, $vfonction, $vportable, $vemail)
    {
		$this->ReqInsert->execute(array(':ADMIN_NOM' => $vnom , ':ADMIN_PRENOM' => $vprenom, ':ADMIN_FONCTION' => $vfonction, ':ADMIN_TEL' => $vportable, ':ADMIN_EMAIL' => $vemail));
    	
        return $this->ReqTrouverDernierID->fetch();
    }
    
    public function TrouverID ($vlogin, $vmdp)
    {
    	$this->ReqTrouverID->execute(array($vlogin, $vmdp));
    	return $this->ReqTrouverID->fetch();
    	
    }
    
    public function UpdateAllById($vnom, $vprenom, $vtel, $vemail, $vid)
    {
    	return $this->ReqUpdate->execute(array($vnom, $vprenom, $vtel, $vemail, $vid));
    }

    public function selectAll()
    {
    	$this->ReqSelectAll->execute(array($id));
        return $this->ReqSelectAll->fetchAll();
    }

	public function selectById($id)
    {
        $this->ReqSelectById->execute(array($id));
        return $this->ReqSelectById->fetchAll();
    }

    public function selectByName($name)
    {
        $this->ReqSelectByName->execute(array('%'.$name.'%'));
        return $this->ReqSelectByName->fetchAll();
    }
    
	public function SelectPhotoProfilByAdminId($id)
	{
		$this->ReqSelectPhotoProfilByAdminId->execute(array($id));
		return $this->ReqSelectPhotoProfilByAdminId->fetch();
	}
	
	public function SelectIdentifiantsById ($id)
	{
		$this->ReqSelectIdentifiantsById->execute(array($id));
		return $this->ReqSelectIdentifiantsById->fetch();
	}
	
}